import {Injectable} from "@angular/core";
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';

@Injectable()
export class TeamManagerService {
  userServeUrl = 'https://accountserver.lessonplan.cn/';//用户相关的Url地址
  teamServeUrl = 'https://teammanageserver.lessonplan.cn/';//团队相关地址
  packetServeUrl = 'https://templateserver.lessonplan.cn/';//教案包相关地址
  constructor(private http: HttpClient,) {
  }

  /*根据用户Guid获取用户详细信息*/
  getUserInfo(userGuid: string): Promise<any> {
    return this.http.get(this.userServeUrl + 'open/getThisUser', {
      params: new HttpParams().set('PK_UserGuid', userGuid)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*获取所有团队列表*/
  getTeamList(userGuid: string): Promise<any> {
    return this.http.get(this.teamServeUrl + 'team/userTeam', {
      params: new HttpParams().set('FK_userGuid', userGuid)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*修改团队名称*/
  modifyTeamName(data: object): Promise<any> {
    return this.http.put(this.teamServeUrl + 'team/modifyName', data).toPromise().then(res => {
      return res;
    })
  }

  /*判断用户是否为团队管理员*/
  isAdministrator(PK_teamGuid, adminGuid): Promise<any> {
    return this.http.get(this.teamServeUrl + 'team/admin', {
      params: new HttpParams().set('PK_teamGuid', PK_teamGuid).set('adminGuid', adminGuid)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*获取所有团队用户 根据accredit:true of false*/
  getTeamMembers(FK_teamGuid, accredit): Promise<any> {
    return this.http.get(this.teamServeUrl + 'team/members', {
      params: new HttpParams().set('FK_teamGuid', FK_teamGuid).set('accredit', accredit)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*修改团队管理员*/
  modifyAdmin(data): Promise<any> {
    return this.http.put(this.teamServeUrl + 'team/admin', data).toPromise().then(res => {
      return res;
    })
  }

  /*移除用户授权*/
  removeAccredit(data): Promise<any> {
    let data1 = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }), body: data
    };
    return this.http.delete(this.teamServeUrl + 'team/accredit', data1).toPromise().then(res => {
      return res;
    })
  }
  /*移除团队成员*/
  removeMembers(data):Promise<any>{
    let data1 = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }), body: data
    };
    return this.http.delete(this.teamServeUrl+'team/members',data1).toPromise().then(res=>{
      return res;
    })
  }

  /*添加用户授权*/
  addAccredit(data): Promise<any> {
    return this.http.post(this.teamServeUrl + 'team/accredit', data).toPromise().then(res => {
      return res;
    })
  }

  /*获取用户的单个团队信息*/
  getSingleTeam(teamGuid): Promise<any> {
    return this.http.get(this.teamServeUrl + 'team', {
      params: new HttpParams().set('PK_teamGuid', teamGuid)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*根据用户账号和密码来获取用户Guid*/
  getUserGuid(data): Promise<any> {
    return this.http.post(this.userServeUrl + 'login_remote', data).toPromise().then(res => {
      return res;
    })
  }

  /*获取所有的教案包(包括副本)*/
  getAllPacket(teamGuid): Promise<any> {
    return this.http.get(this.packetServeUrl+'MyPacket/MotherMyPacket/'+teamGuid).toPromise().then(res => {
      return res;
    })
  }
  /*获取所有的团队应用*/
  getAllApp(teamGuid):Promise<any> {
    return this.http.get(this.packetServeUrl+'MyApp/AllMyAppList/'+teamGuid).toPromise().then(res=>{
      return res;
    })
  }
  /*创建教案包副本*/
  createDuplicate(teamGuid,packetGuid):Promise<any>{
    let data = {
      CreatorGuid:teamGuid,
      MyPacketGuid:packetGuid
    };
    return this.http.post(this.packetServeUrl+'MyPacket/CopyMyPacket',data).toPromise().then(res=>{
       return res;
    })
  }
/*删除教案包副本*/
 deleteDuplicate(duplicateGuid):Promise<any>{
    return this.http.delete(this.packetServeUrl+'MyPacket/RemoveMyPacket/'+duplicateGuid).toPromise().then(res=>{
       return res;
    })
 }
 /*删除图片*/
  deleteIMG(url:string):Promise<any>{
    return this.http.delete('https://upload.lessonplan.cn/clear?imgUrl='+url).toPromise().then(res=>{
      return res;
    })
  }
  /*修改教案包副本*/
  modifyDuplicate(duplicate):Promise<any>{
    let data = {
        PK_MyPacketGuid:duplicate.packetGuid,
        PacketName:duplicate.name,
        EnPacketName:duplicate.enName,
        CoverUrl:duplicate.cover,
        Publisher:duplicate.publisher
    };
     return this.http.put(this.packetServeUrl+'MyPacket/MyPacket',data).toPromise().then(res=>{
       return res;
     })
  }
/*根据教案包Guid获取所有的教案包包括副本*/
 onPacketGuidGetPacket(packetGuid):Promise<any>{
     return this.http.get(this.packetServeUrl+'MyPacket/MyPacketAndCopy/'+packetGuid).toPromise().then(res=>{
       return res;
     })
 }
/*添加团队成员*/
addMembers(teamGuid,members){
  let data ={
    FK_teamGuid:teamGuid,
    members:members
  };
  return this.http.post(this.teamServeUrl+'team/addMembers',data).toPromise().then(res=>{
     return res;
  })
}
}



